package com.songshuai.sales.tool.service.impl;

import com.songshuai.sales.tool.dao.tool.LabelMapper;
import com.songshuai.sales.tool.dao.tool.LabelSopRelationMapper;
import com.songshuai.sales.tool.dao.tool.SopMapper;
import com.songshuai.sales.tool.dto.request.sop.*;
import com.songshuai.sales.tool.dto.response.login.LoginUserResDto;
import com.songshuai.sales.tool.dto.response.sop.ArticleSopResDto;
import com.songshuai.sales.tool.dto.response.sop.ListResDto;
import com.songshuai.sales.tool.service.SopService;
import com.songshuai.sales.tool.util.PageUtil;
import com.songshuai.sales.tool.util.Util;
import com.songshuai.webapi.baseapi.constant.miniprogram.CacheKeys;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.enumerations.sales.SopStatusEnum;
import com.songshuai.webapi.baseapi.enumerations.sales.error.SopError;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.model.sales.Label;
import com.songshuai.webapi.baseapi.model.sales.LabelSopRelation;
import com.songshuai.webapi.baseapi.model.sales.Sop;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SopServiceImpl extends BaseServiceImpl implements SopService  {


    @Autowired
    private SnowFlakeUtil snowFlakeUtil;
    @Autowired
    private SopMapper sopMapper;
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private LabelSopRelationMapper labelSopRelationMapper;



    //查找主子标签（包含自身）--mysql版
    private List<Integer> findSubLabelIds(Integer labelId){

        if(null == labelId){
            return null;
        }

        List<Integer> all = null;
        String str = sopMapper.selectSubLabelSql(labelId);
        if(Util.IsNullAndEmpty(str)){
            return all;
        }

        all = new ArrayList<Integer>();
        str = str.substring(1);
        String[] allStr = str.split(",");
        for(String s:allStr){
            all.add(Integer.parseInt(s));
        }
        return all;
    }


    //返回子标签列表
    private List<String> getLabelList(String labelId){

        if(labelId == null){
            return null;
        }

        Label label =  labelMapper.selectByPrimaryKey(labelId);
        if(label == null){
            return null;
        }
        List<String> result = null;

        //是否为组标签
        if("0".equals(label.getParentId())){
            Label labelObj = new Label();
            labelObj.setParentId(labelId);
            List<Label> labels = labelMapper.selectByEntityList(labelObj);
            if(labels == null || labels.size() == 0){
                return null;
            }
            result = labels.stream().map(item->item.getLabelId()).collect(Collectors.toList());

        }else{
            result = new ArrayList<String>();
            result.add(labelId);
        }
        return result;
    }

    @Override
    public List<ListResDto> showList(BaseParmDto<ListReqDto> request) {

        //获取当前登录用户
        LoginUserResDto loginUserResDto = this.getCurrentUser();
        if(null == loginUserResDto){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAILOfficial);
        }


        try{

            //主子标签
            //List<Integer> labelList = this.findSubLabelIds(request.getParam().getLabelId());
            request.getParam().setLabelIds(this.getLabelList(request.getParam().getLabelId()));

            List<ListResDto> lists = sopMapper.selectSopList(request.getParam());
            return lists;

        }catch (Exception e) {
            log.error("api/sop/showList", e);
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }


    }



    //添加sop校验
    private void addSopCheck(AddSopReqDto addSopReqDto){

        //参数校验
        if(addSopReqDto.getStatus()!= null && addSopReqDto.getStatus() != SopStatusEnum.SopAddSopStatus.getCode() && StringUtils.isBlank(addSopReqDto.getSopId())){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }


        if(null == addSopReqDto.getInitHotNum() ) {
            throw new BusinessException(SopError.ERROR_SOP_HOTNUM_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        if(Util.IsNullAndEmpty(addSopReqDto.getDescription())) {
            throw new BusinessException(SopError.ERROR_SOP_DESCRIPTION_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        if(Util.IsNullAndEmpty(addSopReqDto.getLabels())){
            throw new BusinessException(SopError.ERROR_SOP_LABEL_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }


        if( Util.IsNullAndEmpty(addSopReqDto.getSopGroup()) ){
            throw new BusinessException(SopError.ERROR_SOP_GROUP_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }


        if( Util.IsNullAndEmpty(addSopReqDto.getSubType()) ){
            throw new BusinessException(SopError.ERROR_SOP_SUBTYPE_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        if( Util.IsNullAndEmpty(addSopReqDto.getSopType()) ){
            throw new BusinessException(SopError.ERROR_SOP_SOPTYPE_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        if( Util.IsNullAndEmpty(addSopReqDto.getTitle()) ){
            throw new BusinessException(SopError.ERROR_SOP_TITLE_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        if( Util.IsNullAndEmpty(addSopReqDto.getImg()) ){
            throw new BusinessException(SopError.ERROR_SOP_IMG_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        if( Util.IsNullAndEmpty(addSopReqDto.getSopContent())){
            throw new BusinessException(SopError.ERROR_SOP_CONTENT_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        //视频校验
//        if(addSopReqDto.getSopType().equals(SopStatusEnum.Video.getCode()+"")  && Util.IsNullAndEmpty(addSopReqDto.getVideoKey()) ){
//            throw new BusinessException(SopError.ERROR_SOP_VIDEO_KEY_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
//        }



    }

    @Override
    @Transactional
    public void addSop(AddSopReqDto addSopReqDto) {

        //操作人
        LoginUserResDto loginUserResDto = this.getCurrentUser();
        if(null == loginUserResDto){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAILOfficial);
        }
        addSopReqDto.setCreateBy(loginUserResDto.getUserId());

        //参数校验
        this.addSopCheck(addSopReqDto);

        String[] labels = addSopReqDto.getLabels().split(",");
        if(labels == null || labels.length == 0){
            throw new BusinessException(SopError.ERROR_SOP_LABEL_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }


        try {

            //插入sop记录
            Sop sop = new Sop();
            PropertiesUtil.copyProperties(addSopReqDto, sop);
            sop.setSopId(snowFlakeUtil.nextId());
            sopMapper.insertSelective(sop);
            if(null != sop.getSourceId()){
                Integer useNum = 0;
                String useNumKey = CacheKeys.getPhpSopUseNum(sop.getSourceId());
                if(redisUtil.hasKey(useNumKey)){
                    useNum = Integer.valueOf(redisUtil.getString(useNumKey));
                }
                useNum++;
                redisUtil.set(useNumKey, String.valueOf(useNum));
            }
            //插入标签关系

            if(labels != null && labels.length > 0) {

                LabelSopRelation labelSopRelation = null;
                for (String label : labels) {
                    labelSopRelation = new LabelSopRelation();
                    labelSopRelation.setId(snowFlakeUtil.nextId());
                    labelSopRelation.setLabelId(label);
                    labelSopRelation.setSopId(sop.getSopId());
                    labelSopRelation.setCreateTime(new Date());
                    labelSopRelation.setCreateBy(loginUserResDto.getUserId());
                    labelSopRelationMapper.insertSelective(labelSopRelation);
                }

            }

        }catch (Exception e) {
            log.error("api/sop/addSop", e);
            throw new BusinessException(ResultStatus.ERROR_CREATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }


    }


    @Override
    @Transactional
    public void editSop(AddSopReqDto addSopReqDto) {

        //操作人
        LoginUserResDto loginUserResDto = this.getCurrentUser();
        if(null == loginUserResDto){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAILOfficial);
        }
        addSopReqDto.setCreateBy(loginUserResDto.getUserId());

        //操作时间
        addSopReqDto.setUpdateTime(new Date());


        //参数校验
        this.addSopCheck(addSopReqDto);

        String[] labels = addSopReqDto.getLabels().split(",");
        if(labels == null || labels.length == 0){
            throw new BusinessException(SopError.ERROR_SOP_LABEL_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }
        Sop sop = sopMapper.selectByPrimaryKey(addSopReqDto.getSopId());
        if(null == sop || sop.getIsDelete() == 1){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }


        try {

            //删除sopId标签
            LabelSopRelation labelSopRelationDel = new LabelSopRelation();
            labelSopRelationDel.setSopId(addSopReqDto.getSopId());
            List<LabelSopRelation> labelSopRelations =  labelSopRelationMapper.selectByEntityList(labelSopRelationDel);

            for(LabelSopRelation relation:labelSopRelations){
                labelSopRelationMapper.deleteByPrimaryKey(relation.getId());
            }


            //插入sop记录

            PropertiesUtil.copyProperties(addSopReqDto, sop);
            sopMapper.updateByPrimaryKey(sop);

            //插入标签关系
            if(labels != null && labels.length > 0) {

                LabelSopRelation labelSopRelation = null;
                for (String label : labels) {
                    labelSopRelation = new LabelSopRelation();
                    labelSopRelation.setId(snowFlakeUtil.nextId());
                    labelSopRelation.setLabelId(label);
                    labelSopRelation.setSopId(sop.getSopId());
                    labelSopRelation.setCreateTime(new Date());
                    labelSopRelation.setCreateBy(loginUserResDto.getUserId());
                    labelSopRelationMapper.insertSelective(labelSopRelation);
                }

            }

        }catch (Exception e) {
            log.error("api/sop/editSop", e);
            throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }


    }

    @Override
    @Transactional
    public void deleteSop(DeleteSopReqDto deleteSopReqDto) {

        //操作人
        LoginUserResDto loginUserResDto = this.getCurrentUser();
        if(null == loginUserResDto){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAILOfficial);
        }
        deleteSopReqDto.setUpdateBy(loginUserResDto.getUserId());
        deleteSopReqDto.setUpdateTime(new Date()); //更新时间

        //参数校验
        if( Util.IsNullAndEmpty(deleteSopReqDto.getSopId()) ){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        Sop sopCheck = sopMapper.selectByPrimaryKey(deleteSopReqDto.getSopId());
        if(sopCheck == null || sopCheck.getIsDelete() == 1){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }


        try {

            //本人创建-硬删除
            if(loginUserResDto.getUserId().equals(sopCheck.getCreateBy())){

                //删除sopId标签
                LabelSopRelation labelSopRelationDel = new LabelSopRelation();
                labelSopRelationDel.setSopId(deleteSopReqDto.getSopId());
                List<LabelSopRelation> labelSopRelations =  labelSopRelationMapper.selectByEntityList(labelSopRelationDel);

                if(labelSopRelations != null && labelSopRelations.size() > 0) {
                    for (LabelSopRelation relation : labelSopRelations) {
                        labelSopRelationMapper.deleteByPrimaryKey(relation.getId());
                    }
                }

                sopMapper.deleteByPrimaryKey(deleteSopReqDto.getSopId());

            }else{

                //非本人创建-软删除
                //删除sop记录
                Sop sop = sopMapper.selectByPrimaryKey(deleteSopReqDto.getSopId());
                PropertiesUtil.copyProperties(deleteSopReqDto, sop);
                sop.setIsDelete(SopStatusEnum.Del.getCode());
                sopMapper.updateByPrimaryKey(sop);
            }



        }catch (Exception e) {
            log.error("api/sop/deleteSop", e);
            throw new BusinessException(ResultStatus.ERROR_DELETE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }

    @Override
    public ArticleSopResDto articleSop(ArticleSopReqDto articleSopReqDto) {


        //参数校验
        if( Util.IsNullAndEmpty(articleSopReqDto.getSopId()) ){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        Sop sopCheck = sopMapper.selectByPrimaryKey(articleSopReqDto.getSopId());
        if(sopCheck == null || sopCheck.getIsDelete() == 1){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }



        try{

            ArticleSopResDto articleSopResDto = new ArticleSopResDto();

            PropertiesUtil.copyProperties(sopCheck, articleSopResDto);
            articleSopResDto.setCreateTime(sopCheck.getCreateTime());
            articleSopResDto.setUpdateTime(sopCheck.getUpdateTime());
            //标签属性
            LabelSopRelation labelSopRelation = new LabelSopRelation();
            labelSopRelation.setSopId(articleSopReqDto.getSopId());
            List<Label> labels =  labelSopRelationMapper.selectLabelsForSopId(labelSopRelation);
            articleSopResDto.setLabels(labels);
            return articleSopResDto;

        }catch (Exception e) {
            log.error("api/sop/articleSop", e);
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }

    }


    /*是否有同标题的发布文章*/
    private boolean isHaveTitle(String title){

        Sop sop = new Sop();
        sop.setIsPublish(SopStatusEnum.Publish.getCode());
        sop.setTitle(title);
        sop.setIsDelete(SopStatusEnum.UnDel.getCode());
        sop = sopMapper.selectTitleByEntity(sop);
        if(sop == null){
            return false;
        }
        return true;
    }

    @Override
    public void sopStatusSet(SopStatusSetReqDto sopStatusSetReqDto) {

        //操作人
        LoginUserResDto loginUserResDto = this.getCurrentUser();
        if(null == loginUserResDto){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAILOfficial);
        }
        sopStatusSetReqDto.setUpdateBy(loginUserResDto.getUserId());
        sopStatusSetReqDto.setUpdateTime(new Date()); //更新时间

        //参数校验
        if( Util.IsNullAndEmpty(sopStatusSetReqDto.getSopId()) ){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        //延时发布，时间判断
        if(null!=sopStatusSetReqDto.getIsPublish() && sopStatusSetReqDto.getIsPublish() == SopStatusEnum.DelayedPublish.getCode() && null == sopStatusSetReqDto.getPublishTime()){
            throw new BusinessException(SopError.ERROR_SOP_PUBLISHTIME_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }


        //置顶结束时间判断
        if(null!=sopStatusSetReqDto.getIsTop() && sopStatusSetReqDto.getIsTop() == SopStatusEnum.Top.getCode() && null == sopStatusSetReqDto.getTopEndTime()){
            throw new BusinessException(SopError.ERROR_SOP_TOPENDTIME_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }


        Sop sopCheck = sopMapper.selectByPrimaryKey(sopStatusSetReqDto.getSopId());
        if(sopCheck == null || sopCheck.getIsDelete() == SopStatusEnum.Del.getCode()){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }

        //直接发布
        if(null!=sopStatusSetReqDto.getIsPublish() && sopStatusSetReqDto.getIsPublish() == SopStatusEnum.Publish.getCode()){
            if(this.isHaveTitle(sopCheck.getTitle())){
                throw new BusinessException(SopError.ERROR_SOP_SAME_TITLE_IS_EXIST.getMsg(), ResultStatus.FAILOfficial);

            }
        }

        try{

            //如果延迟发布
            if(null!=sopStatusSetReqDto.getIsPublish() && sopStatusSetReqDto.getIsPublish() == SopStatusEnum.DelayedPublish.getCode()){
                sopStatusSetReqDto.setPublishByName(loginUserResDto.getTrueName());
                redisUtil.set(SopStatusEnum.Publish_.getMsg()+sopStatusSetReqDto.getSopId(),SopStatusEnum.Default.getCode(),Util.calcSecond(sopStatusSetReqDto.getPublishTime()));
            }

            //发布
            if(null!=sopStatusSetReqDto.getIsPublish() && sopStatusSetReqDto.getIsPublish() == SopStatusEnum.Publish.getCode()){
                sopStatusSetReqDto.setPublishByName(loginUserResDto.getTrueName());
                sopStatusSetReqDto.setPublishTime(Util.ChangeDateToString(new Date(),null));
            }

            //取消发布
            if(null!=sopStatusSetReqDto.getIsPublish() && sopStatusSetReqDto.getIsPublish() == SopStatusEnum.UnPublish.getCode()){
                if(redisUtil.hasKey(SopStatusEnum.Publish_.getMsg()+sopStatusSetReqDto.getSopId())){
                    redisUtil.del(SopStatusEnum.Publish_.getMsg()+sopStatusSetReqDto.getSopId());
                }
            }


            //如果设置置顶结束时间
            if(null!=sopStatusSetReqDto.getIsTop() && sopStatusSetReqDto.getIsTop() == SopStatusEnum.Top.getCode()){
                sopStatusSetReqDto.setTopStartTime(new Date());
                redisUtil.set(SopStatusEnum.Top_.getMsg()+sopStatusSetReqDto.getSopId(),SopStatusEnum.Default.getCode(),Util.calcSecond(sopStatusSetReqDto.getTopEndTime()));
            }


            PropertiesUtil.copyProperties(sopStatusSetReqDto, sopCheck);
            sopMapper.updateByPrimaryKey(sopCheck);

        }catch (Exception e) {
            log.error("api/sop/sopStatusSet", e);
            throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }

    }

    @Override
    @Transactional
    public void sopTagSet(SopTagSetReqDto sopTagSetReqDto) {

        //操作人
        LoginUserResDto loginUserResDto = this.getCurrentUser();
        if(null == loginUserResDto){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAILOfficial);
        }
        sopTagSetReqDto.setUpdateBy(loginUserResDto.getUserId());
        sopTagSetReqDto.setUpdateTime(new Date()); //更新时间

        //参数校验
        if( Util.IsNullAndEmpty(sopTagSetReqDto.getSopId()) ){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        if( Util.IsNullAndEmpty(sopTagSetReqDto.getLabels()) ){
            throw new BusinessException(SopError.ERROR_SOP_LABEL_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        String[] labels = sopTagSetReqDto.getLabels().split(",");
        if(labels == null || labels.length == 0){
            throw new BusinessException(SopError.ERROR_SOP_LABEL_NOT_NULL.getMsg(), ResultStatus.FAILOfficial);
        }

        Sop sopCheck = sopMapper.selectByPrimaryKey(sopTagSetReqDto.getSopId());
        if(sopCheck == null || sopCheck.getIsDelete().intValue() == 1){
            throw new BusinessException(SopError.ERROR_SOP_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }



        try {

            //删除sopId标签
            /*
            LabelSopRelation labelSopRelationDel = new LabelSopRelation();
            labelSopRelationDel.setSopId(sopTagSetReqDto.getSopId());
            List<LabelSopRelation> labelSopRelations =  labelSopRelationMapper.selectByEntityList(labelSopRelationDel);

            for(LabelSopRelation relation:labelSopRelations){
                labelSopRelationMapper.deleteByPrimaryKey(relation.getId());
            }
            */

            //插入标签关系
            if(labels != null && labels.length > 0) {

                LabelSopRelation labelSopRelation = null;
                for (String label : labels) {
                    labelSopRelation = new LabelSopRelation();
                    labelSopRelation.setId(snowFlakeUtil.nextId());
                    labelSopRelation.setLabelId(label);
                    labelSopRelation.setSopId(sopTagSetReqDto.getSopId());
                    labelSopRelation.setCreateTime(new Date());
                    labelSopRelation.setCreateBy(loginUserResDto.getUserId());
                    labelSopRelationMapper.insertSelective(labelSopRelation);
                }

            }

        }catch (Exception e) {
            log.error("api/sop/sopTagSet", e);
            throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }

    }

    @Override
    public void SopRedisWarn(String str) {

        //sales:top_1
        //sales:publish_1
        log.info("api/sop/redis/expire{}", "rediskey:"+str);


        try {

            if(StringUtils.isBlank(str)){
                return;
            }
            //置顶到期
            if(str.startsWith(SopStatusEnum.PrefixTop.getMsg())){
                String[] key = str.split("_");
                if(key!=null && key.length>1){
                    Sop sop = sopMapper.selectByPrimaryKey(key[1]);
                    sop.setIsTop(SopStatusEnum.UnTop.getCode());
                    sopMapper.updateByPrimaryKey(sop);
                }
            }


            //延迟发布
            if(str.startsWith(SopStatusEnum.PrefixPublish.getMsg())){
                String[] key = str.split("_");
                if(key!=null && key.length>1){
                    Sop sop = sopMapper.selectByPrimaryKey(key[1]);
                    sop.setIsPublish(SopStatusEnum.Publish.getCode());
                    sopMapper.updateByPrimaryKey(sop);
                }
            }

        }catch (Exception e) {
            log.error("api/sop/redis/expire", e);
        }
    }

    @Override
    public void updateSopMainType(String mainType, String subType) {
        sopMapper.updateSopMainType(mainType, subType);
    }

    @Override
    public Integer querySopCountByTypeId(String typeId) {
        Integer count = sopMapper.querySopCountByTypeId(typeId);
        return count;
    }
}
